﻿HmJS.$Import('core.element.event');
HmJS.$Import('core.json');
//HmJS.$Import('core.domready');
HmJS.$Import('util.array.base');

HmJS.register('demo.module.types.arraysorton', function ($) {
	var that = {};

	var pretty_print_data = function (data) {
		return JSON.encode(data).replace(/(\},)/g, '},\n').replace(/\[\{/g, '[\n{').replace(/\}\]/g, '}\n]').replace(/\{/g, '  {');
	};

	var display_data = function (data, owner) {
		owner.getParent('.sample').getElement('.output_area').set('text', pretty_print_data(data));
	};

	that.doSort = function () {

		document.addEvent('domready', function () {
			document.id('name_sort').addEvent('click', function () {
				prime_data();
				test_data.sortOn("name");
				display_data(test_data, this);
			});

			document.id('name_i_sort').addEvent('click', function () {
				prime_data();
				test_data.sortOn("name", Array.CASEINSENSITIVE);
				display_data(test_data, this);
			});

			document.id('name_i_d_sort').addEvent('click', function () {
				prime_data();
				test_data.sortOn("name", Array.CASEINSENSITIVE | Array.DESCENDING);
				display_data(test_data, this);
			});

			document.id('city_sort').addEvent('click', function () {
				prime_data();
				test_data.sortOn("city");
				display_data(test_data, this);
			});

			document.id('city_name_sort').addEvent('click', function () {
				prime_data();
				test_data.sortOn(["city", "name"]);
				display_data(test_data, this);
			});

			document.id('name_city_sort').addEvent('click', function () {
				prime_data();
				test_data.sortOn(["name", "city"]);
				display_data(test_data, this);
			});

			document.id('name_city_2_sort').addEvent('click', function () {
				prime_data();
				test_data.sortOn(["name", "city"], [Array.CASEINSENSITIVE, Array.DESCENDING]);
				display_data(test_data, this);
			});

			document.id('zip_sort').addEvent('click', function () {
				prime_data();
				test_data.sortOn("zip");
				display_data(test_data, this);
			});

			document.id('zip_n_sort').addEvent('click', function () {
				prime_data();
				test_data.sortOn("zip", Array.NUMERIC);
				display_data(test_data, this);
			});
		});
	};

	return that;
});